/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/




#ifndef EVENTSERVER_H
#define EVENTSERVER_H

// Qt
#include <QThread>
#include <QLinkedList>

// gcommon
#include "gexception.h"

// gparams
#include "gparams.h"

// gnet
#include "gnetudp.h"
#include "gnetpackets.h"

// libautonet
#include "autopackets.h"


#define BufferItem QPair< quint64, QByteArray >

// struct BufferItem
// {
// 	quint64 timestamp;
// 	QByteArray image;
// };




// ------------------------- EventServer{} -----------------------------------
// ---------------------------------------------------------------------------
class EventServer : public QThread
{
	Q_OBJECT

public:

	EventServer( QObject * pParent = 0 ) : QThread( pParent ) {}
	virtual ~EventServer() {}

	
	/// Entry processing loop
	void run();

	/// Read jpeg 2k image from camera
	///
	/// \a shrink describes shrinking level.
	QByteArray readJpeg( uint64_t * timestamp, quint8 shrink = 0 )const;
	
	/// Sends JPEG via UDP
	void sendJpeg( const QByteArray & jpeg,
				   const QHostAddress& address,
	   			   quint16 port
				 );

	void addImageToBuffer( const QByteArray & image, uint64_t timestamp );

	/// Processes received packet
	void processPacket( GNetPacket * pPacket );
	
	/// Processes packet EventTrigger
	void process_PACKET_EVENT_TRIGGER( GAutoEventTrigger * pPacket );
	
protected slots:
	/// Read udp event trigger.
	void udpPacketsReceived( const QQueue< QPair<quint16, QByteArray> > &packets );

private:
	void init();
	
	void readParams( const GParams & params );
	
	BufferItem previousImage( const quint16 quint16 )const;
	BufferItem nextImage( const quint16 quint16 )const;
	
	
	GUdpSender	 _udpSender;    ///< UDP sender tool
	GUdpReceiver _udpReceiver;  ///< UDP receiver tool
	quint32	     _image_id;     ///< Current id image
	
	quint16      _jpegSendingInterval; ///< Timeout for sending jpegs through udp
	QHostAddress _coCameraIP;          ///< First autostop camera ip

	QLinkedList< BufferItem > _imagesBuffer; /// Images buffer
	
}; // class !EventServer
// ---------------------------------------------------------------------------




#endif // !EVENTSERVER_H

// eof
